import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Zuseduilie {
    public static void main(String[] args) throws InterruptedException {
        BlockingDeque<String> queue=new LinkedBlockingDeque<String>(10);


        Markpople markpople1=new Markpople(queue);
//        Markpople markpople2=new Markpople(queue);
//        Markpople markpople3=new Markpople(queue);
        Moneypople moneypople=new Moneypople(queue);

        ExecutorService service= Executors.newCachedThreadPool();

        service.execute(markpople1);
//        service.execute(markpople2);
//        service.execute(markpople3);
        service.execute(moneypople);

        Thread.sleep(10000);
        markpople1.stop();
//        markpople2.stop();
//        markpople3.stop();


        Thread.sleep(2000);
        service.shutdown();

    }
}



//生产者
class Markpople implements Runnable{

    private  volatile boolean isrunning=true;
    private BlockingDeque queue;
    private static AtomicInteger count =new AtomicInteger();
    private static final int DEFAULT_RANGE_FOR_SLEEP=1000;

    Markpople(BlockingDeque queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        String data=null;
        Random random=new Random();

        System.out.println("启动生产者");
        try {
            while(isrunning){
                System.out.println("生产数据");
                    Thread.sleep(random.nextInt(DEFAULT_RANGE_FOR_SLEEP));
                    data="data:"+count.incrementAndGet();
                System.out.println("生产了数据："+data);
                    if(!queue.offer(data,2, TimeUnit.SECONDS)){
                        System.out.println("放入失败:"+data);
                    }
            }
        }                catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println("退出生产者");
        }
    }

    public void stop(){
        isrunning=false;
    }
}


//消费者
class Moneypople implements Runnable{

    private BlockingDeque queue;
    private static  final  int DEFAULT_RANGE_FOR_SLEEP=1000;


    Moneypople(BlockingDeque queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        System.out.println("启动消费者线程");
        Random r=new Random();
        boolean isrunning=true;

        try {
            while(isrunning){
                System.out.println("正在获取数据");
                String data= (String) queue.poll(2,TimeUnit.SECONDS);
                if(data!=null){
                    System.out.println("拿到了数据："+data);
                    System.out.println("正在使用数据"+data);
                    Thread.sleep(r.nextInt(DEFAULT_RANGE_FOR_SLEEP));
                }else {
                    isrunning=false;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println("退出消费者");
        }
    }
}